package com.xszx.knowledge.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.knowledge.dto.PageReq;
import com.xszx.knowledge.dto.UserDTO;
import com.xszx.knowledge.entity.User;
import com.xszx.knowledge.service.UserService;
import com.xszx.knowledge.util.Result;
import com.xszx.knowledge.util.SnowFlake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author wencai
 * @since 2023/04/25 21:00
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private SnowFlake snowFlake;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 进行数据分页的查询
     * @return
     */
    @ApiOperation(value = "用户分页")
    @GetMapping("/getPage")
    public Result getPageUser(PageReq pageReq){
        //指定当前页和每页显示数据的条数
        Page page = new Page(pageReq.getCurrent(),pageReq.getSize());
        //进行数据的查询
        IPage<User> userIPage = userService.page(page);
        return Result.ok(userIPage);
    }

    /**
     * 定义添加或者更新的接口
     */
    @ApiIgnore
    @PostMapping("/save")
    public Result save(@Valid @RequestBody User user){
        //密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        System.out.println(user.getPassword());
       return userService.saveUser(user);
    }
    /**
     * 定义删除接口
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable long id){
        return Result.ifSuccess(userService.removeById(id));
    }
    /**
     * 定义重置密码的接口
     */
    @PostMapping("/resetPassword")
    public Result resetPassword(@Valid @RequestBody UserDTO userDTO) {
        //密码后台加密
        String resetPsw = DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes());
        return Result.ifSuccess(userService.updateById(new User(userDTO.getId(), resetPsw)));
    }
    /**
     * 登录操作
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        //调用业务层的方法获取登录成功的user对象
        User user = userService.login(userDTO);
        //生成token
        Long token = snowFlake.nextId();//雪花算法生成唯一的token
        //将用户信息和token一起保存在redis中
       // redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSON(user),3600 * 24, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSON(user),3600 * 24, TimeUnit.SECONDS);
        //将登录成功后的信息返回给前端
        UserDTO userDto = new UserDTO(user.getId(),user.getLoginName(),user.getName(),token.toString());
        return Result.ok(userDto);
    }

    /**
     * 退出登录
     */
    @GetMapping("/logout/{token}")
    public Result login(@PathVariable String token){
        //将用户的登录信息从redis中删除
       redisTemplate.delete(token);
        return Result.ok();
    }
}
